<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> 
        .box {
            width: 300px;
            margin: 100px auto;
            text-align: center;
        }

        span {
            color: #999;
            font-size: 12px;
        }

        i {
            display: inline-block;
            background-image: url('../images/ico.png');
            background-position: 0 0;
            width: 16px;
            height: 16px;
            vertical-align: middle;
        }

    </style>
</head>
<body>
    <div class="box">
        <input type="text">
        <i class="zq"></i>
        <span>请输入6-16位密码</span>
    </div>
    <script>
        var inputTF = document.querySelector('input');
        var icon = document.querySelector('i');
        var text = document.querySelector('span');
        //给表单添加时区焦点的事件
        inputTF.onblur = function() {
            if (this.value === '') {
                icon.style.backgroundPosition = '0 0';
                text.style.color = '#999';
                text.innerText = '请输入6-16位密码';
            } else {
                if (this.value.length >= 6 && this.value.length <= 16) {
                    icon.style.backgroundPosition = '-30px 0';
                    text.style.color = 'green';
                    text.innerText = '密码符合要求';
                } else {
                    icon.style.backgroundPosition = '-60px 0';
                    text.style.color = 'red';
                    text.innerText = '请按要求输入密码';
                }
            }
        }
    </script>
</body>
</html>